package com.zha;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCutils {
    private static String driver = "com.mysql.jdbc.Driver";

    private static String url = "jdbc:mysql://localhost:3306/txt?characterEncoding=utf-8&&useSSL=false";
    private static String username = "root";   //用户名
    private static String password = "root";   //密码
    private static Connection conn = null;

    public static Connection getConnection(){
        if (conn == null) {
            try {
                //第一步：加载驱动
                Class.forName(driver);
                //创建数据库连接   调用DriverManager.getConnection(url, username, password)方法
                conn = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }
}
